<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <title>管理员用户 </title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="../extjs-4.1.0/resources/css/ext-all.css" />
<script type="text/javascript" src="../extjs-4.1.0/ext-all.js"></script>
<script type="text/javascript" src="../js/head.js"></script>
<script type="text/javascript" src="../js/ux/form/SearchField.js"></script>
<script type="text/javascript" src="../js/ux/RowExpander.js"></script>
<script src="../js/jquery-1.7.2.min.js" type="text/javascript"></script>
<script type="text/javascript" charset="utf-8" src="../js/i18n.js"></script>
 
    <style type="text/css">
        body .x-panel {
            margin:0 0 20 0;
			padding:0 0 0 0;
        }
        .x-form-unit {  
            height: 22px;  
            line-height: 22px;  
            padding-left: 2px;  
            display: inline-block;  
            display: inline;  
        }  
    </style>

<script type="text/javascript">

Ext.Loader.setConfig({
    enabled: true
});

Ext.require([
    'Ext.grid.*',
    'Ext.data.*',
    'Ext.ux.RowExpander',
	'Ext.ux.form.SearchField'
	// 'Ext.selection.CheckboxModel'
    
]);

// var sm = Ext.create('Ext.selection.CheckboxModel');
Ext.onReady(function() {
    var params;
    MultiLang = (function() {
        return {
            init: function() {
                // load ExtJS locale
                params = getCookie("lang");
                i18n.set({
  				  lang: params, 
  				  path: '../resources'
  				});
                if (params) {
                    var url = Ext.util.Format.format('../extjs-4.1.0/locale/ext-lang-{0}.js', params);
                    Ext.Ajax.request({
                        url: url,
                        success: this.onLoadExtLocaleSuccess,
                        failure: this.onLoadExtLocaleFailure,
                        scope: this
                    });
                } else {
                    // no language found, locale of ExtJS will be english as default
                    //this.loadmyprojectLocale();
                	this.setup();
                }
            },
            onLoadExtLocaleSuccess: function(response) {
                try {
                    eval(response.responseText);
                } catch (e) {
                    Ext.Msg.alert('Failure', e.toString());
                }
                //this.loadmyprojectLocale();
                this.setup();
            },
            onLoadExtLocaleFailure: function() {
                Ext.Msg.alert('Failure', 'Failed to load locale file.');
                this.setup();
                //this.loadmyprojectLocale();
            },
            
            setup: function() {
			     Ext.Ajax.timeout = 120000;  	
// /////////////////////////////////////// 创建数据

Ext.define('node',{
   extend:'Ext.data.Model',
   fields:[
   {name:'id' ,type:'string'},
   {name:'name', type:'string'},
   {name:'cpu', type:'string',mapping:'cpuInfo'},
   {name:'ram', type:'string',mapping:'ramInfo'},
   {name:'disk', type:'string',mapping:'diskInfo'},
   {name:'ip', type:'string'},
   {name:'cpuRate', type:'int'},
   {name:'ramRate', type:'int'},
   {name:'diskRate', type:'int'},
   {name:'desc', type:'string',mapping:'description'}
   ]
});


var v_store=Ext.create('Ext.data.Store', {
    model: 'node',
    sorters: [
              {
                  property : 'id',
                  direction: 'DESC'
              }
          ],
	remoteSort:true,
	proxy: {
        type: 'ajax',
        url : path+'/sc/node!pageNodes.action',
		reader: {
            type: 'json',
			root:'resultObject',
			totalProperty: 'resultObject.totalCount'
        }
    },
	autoLoad: true
});

/* var v_newNodeStore=Ext.create('Ext.data.Store', {
    model: 'node',
	proxy: {
        type: 'ajax',
        url : path+'/sc/node!findNodes.action',
		reader: { 
            type: 'json',
			root:'resultObject',
			totalProperty: 'resultObject.totalCount'
        }
    },
	autoLoad: true
}); */

//store1.sort('id', 'DESC');

var v_height = 30;
var v_width = 290;
var v_margin = '4px';


Ext.define('textPanel',{
	extend:'Ext.panel.Panel',
	height:v_height,
	width:v_width,
	border:false,
	layout:'hbox',
	style:{
		marginTop:v_margin
	},
	items:[
		{
		xtype:'textfield',
		readOnly:true,
		fieldLabel:''
		},
		{
		xtype:'displayfield',
			value:''
			}],
	getValue:function(){
	return this.getComponent(0).getValue();
	},
	setLabel:function(label){
	this.getComponent(0).setFieldLabel(label);
	},
	setExt:function(label){
	this.getComponent(1).setValue(label);
	},
	setValue:function(value){
	this.getComponent(0).setValue(value);
	}
	
});

Ext.define('sliderPanel',{
extend:'Ext.panel.Panel',
	height:v_height,
	width:v_width,
	border:false,
	layout:'hbox',
	items:[
	{
	xtype:'sliderfield',
	fieldLabel: '',
	width:230,
	increment: 1,
    minValue: 1,
    maxValue: 10,
    value:1,
	tipText: function(thumb){
        return Ext.String.format('<b>{0}</b>', thumb.value);
	},
	listeners:{
		'change':{
			fn:function(){
				this.getBubbleTarget().getComponent(1).setValue(this.getValue());
			}
	  	}
	}
    },{
		xtype: 'displayfield',
		margin:'0 0 0 8',
		value:'1'
	},{
	xtype: 'displayfield',
		value:''
	}],
	setValue:function(value){
	this.getComponent(0).setValue(value);
	this.getComponent(1).setValue(value);
	},
	setLabel:function(label){
	this.getComponent(0).setFieldLabel(label);
	},
	getValue:function(){
	return this.getComponent(0).getValue();
	}
});

Ext.define('descPanel',{
	extend:'Ext.panel.Panel',
	height:100,
	width:500,
	border:false,
	layout:'hbox',
	style:{
		marginTop:v_margin
	},
	items:[
		{
		height:50,
		width:430,
		xtype:'textfield',
		fieldLabel:i18n._('ExpensesDesc')
		}],
	setValue:function(value){
	this.getComponent(0).setValue(value);
	},
	getValue:function(){
	return this.getComponent(0).getValue();
	}
	
});

Ext.define('commonForm',{ 
extend:'Ext.form.Panel',	
	            	height:380,
                    border: false,
                    items: [{
					xtype:"fieldset",
					border:true,
					anchor:'100% 100%',
                    style:{
					 marginLeft:"5px",
					 marginRight:"5px",
                     marginTop:"5px"
					}
					}]
                   });


var v_nodeName=Ext.create('textPanel');
v_nodeName.setLabel(i18n._('node name'));

var v_cpu=Ext.create('textPanel');
v_cpu.setLabel('cpu');
v_cpu.setExt(i18n._('core'));
var v_ram=Ext.create('textPanel');
v_ram.setLabel('ram');
v_ram.setExt('G');
var v_disk=Ext.create('textPanel');
v_disk.setLabel('disk');
v_disk.setExt('G');
var v_ip=Ext.create('textPanel');
v_ip.setLabel('ip');
var v_cpuRate=new Ext.create('sliderPanel');
v_cpuRate.setLabel(i18n._('cpu rate'));
var v_ramRate=new Ext.create('sliderPanel');
v_ramRate.setLabel(i18n._('ram rate'));
var v_diskRate=new Ext.create('sliderPanel');
v_diskRate.setLabel(i18n._('disk rate'));

var v_desc= Ext.create('descPanel');

var v_nodeForm = Ext.create('commonForm');		
v_nodeForm.getComponent(0).setTitle(i18n._('node info'));
v_nodeForm.getComponent(0).add([v_nodeName,v_cpu,v_ram,v_disk,v_ip,v_cpuRate,v_ramRate,v_diskRate,v_desc]);

var v_button=Ext.create('Ext.button.Button',{
style:'margin-right:5'
});	

var v_window=Ext.create('Ext.window.Window', {
			//title: '节点发现',
			//title:i18n._('Add_ServiceCatalog'),
	        modal:'true',
            width: 600,
            autoHeight:true,
            layout: 'anchor',
            closable:false,
			items: v_nodeForm,
			tools:[{
			type:'close',
			handler:function(){					
			v_window.getComponent(0).getForm().reset();
			v_window.hide();
			}
			}],               
			buttons: [
			v_button,
			{   
				style:'margin-right:5',
				text: i18n._('Cancel'),
				//text:i18n._('OK'),
				handler:function(){
					v_window.getComponent(0).getForm().reset();
					v_window.hide();
				}
			}]
			});

function ajax_request(v_json){
var v_mask = new Ext.LoadMask(Ext.getBody(), {
		msg:i18n._('saving'),
        removeMask: true //完成后移除
    });
v_mask.show();					
Ext.Ajax.request({
	url: path+'/sc/node!save.action',

	jsonData:v_json,
	success:function(response){
		v_mask.hide();
		v_nodeForm.getForm().reset();
		v_window.hide();
	
		var result=Ext.JSON.decode(response.responseText);
		if(result.success==false){
			Ext.MessageBox.show({
              // title: '提示',
                title:i18n._('notice'),
               msg: result.resultMsg,
               icon:Ext.MessageBox.INFO,	 	           		  
               buttons: Ext.MessageBox.OK	 	                      
		});
		}else{
		v_store.load();
		}
	}	
});	
}
			
function getJson(isAdd,id){
if(isAdd==0){
return {
name:v_nodeName.getValue(),
cpuInfo:v_cpu.getValue(),
ramInfo:v_ram.getValue(),
diskInfo:v_disk.getValue(),
ip:v_ip.getValue(),
cpuRate:v_cpuRate.getValue(),
ramRate:v_ramRate.getValue(),
diskRate:v_diskRate.getValue(),
description:v_desc.getValue()
};
}else{
return {
id:id,
name:v_nodeName.getValue(),
cpuInfo:v_cpu.getValue(),
ramInfo:v_ram.getValue(),
diskInfo:v_disk.getValue(),
ip:v_ip.getValue(),
cpuRate:v_cpuRate.getValue(),
ramRate:v_ramRate.getValue(),
diskRate:v_diskRate.getValue(),
description:v_desc.getValue()
};}
}			
		
function showMsg(title,msg){
	 Ext.MessageBox.show({
         title: title==null?i18n._('Prompt'):title,
          msg: msg==null?i18n._('Input error'):msg,
        icon:Ext.MessageBox.INFO,
       buttons: Ext.MessageBox.OK
       });
}

Ext.QuickTips.init();
Ext.form.Field.prototype.msgTarget = 'side';
 var pluginExpanded = true;
 var dataGrid=Ext.create('Ext.grid.Panel', {
    layout:'fit',
	title:i18n._('physical resource pool management')+'&nbsp;&nbsp;>>&nbsp;&nbsp;'+i18n._('node management'),
    store:v_store,
	simpleSelect:true,
	selModel: Ext.create('Ext.selection.RowModel'),
    columns: [Ext.create('Ext.grid.RowNumberer',
	    {header:i18n._('row_Num'),dataIndex:'id',align:'left',flex:.5}),
		{header: i18n._('node'), dataIndex: 'name',flex:1.1},//
		{header: i18n._('cpu(core num)'), dataIndex: 'cpu',flex:1.1},//
		{header: i18n._('ram(G)'), dataIndex: 'ram',flex:1.1},//
		{header: i18n._('disk(G)'), dataIndex: 'disk',flex:1.1,sortable:true},
        {header: "ip", dataIndex: 'ip',flex:1.1,sortable:true}
	],
	// //////////////
    selType: 'cellmodel',
	bbar: Ext.create('Ext.PagingToolbar', {
          store:v_store,
          displayInfo: true
    }),       
// //////////////////////////////
    viewConfig: {
        plugins: {
            ptype: 'gridviewdragdrop',
            dragText: 'Drag and drop to reorganize'
        }
    },
// //////////////////////////////
	
	 dockedItems:[
			{xtype:'toolbar',
			dock: 'top',			
			items:[			
			  {    
			    margin:'0 0 0 0',
                text:i18n._('found node'),
			   //text:i18n._('Add_ServiceCatalog'),
				style:'margin-left:750px;',               
				icon: '../images/add.png',
				handler:function(){
					var v_mask = new Ext.LoadMask(Ext.getBody(), {
                        msg: i18n._('retriving new node, please wait'),
						//msg:i18n._('ServiceCatalog is saving,please wait!'),
                        removeMask: true //完成后移除
                    });
				v_mask.show();	
					
					var v_newNodeStore=Ext.create('Ext.data.Store', {
					    model: 'node',
						proxy: {
					        type: 'ajax',
					        url : path+'/sc/node!findNodes.action',
							reader: {
					            type: 'json',
								root:'resultObject',
								totalProperty: 'resultObject.totalCount'
					        }
					    },
						autoLoad: true,
						listeners:{
							'load':function(){
								v_mask.hide();
								//var node=new Array();//将未添加的节点放入数组
								if(v_newNodeStore.getCount()>0){
								for(i=0;i<v_newNodeStore.getCount();i++){
									var bool=false;
									if(v_store.getCount()==0){bool=true;}
									for(j=0;j<v_store.getCount();j++){
									if(v_newNodeStore.getAt(i).get('name')==v_store.getAt(j).get('name')){
											bool=false;
										}else{
											bool=true;
										}
									}
									if(bool==true){
									//alert(i);
									var record=v_newNodeStore.getAt(i);
									v_nodeName.setValue(record.get('name'));
									v_cpu.setValue(record.get('cpu'));
									v_ram.setValue(record.get('ram'));
									v_disk.setValue(record.get('disk'));
									v_ip.setValue(record.get('ip'));
									break;
									}else{
									showMsg(null,i18n._('no new node'));
									return;
									}
								}}else{
									showMsg(null,i18n._('no new node'));
									return;
									}
								v_button.setText(i18n._('Add'));				
								v_button.setHandler(
								function(){
								var json=getJson(0,0);
								ajax_request(json);				
								});
								v_window.setTitle(i18n._('found node'));
								v_window.show();
							}
						}
					});
					
					
				
				}
   
            },'-',
			{    
			    margin:'0 0 0 0',
                text:i18n._('modify node'),
				//text:i18n._('Modify_ServiceCatalog'),
				style:'margin-left:750px;',
               
				icon: '../images/refresh.png',
				handler:function(){
				 // 修改套餐
                 var rows=dataGrid.getSelectionModel().getSelection();
				 var scId;
				 if(rows.length > 0){
				 scId=rows[0].get('id');			
					}else{
			    		
						showMsg(null,i18n._('please choose one column')); 
						return;
			    	}

			     	var record=	v_store.getById(scId);
					v_nodeName.setValue(record.get('name'));
					v_cpu.setValue(record.get('cpu'));v_cpu.setExt(i18n._('core'));
					v_ram.setValue(record.get('ram'));v_ram.setExt('G');
					v_disk.setValue(record.get('disk'));v_disk.setExt('G');
					v_ip.setValue(record.get('ip'));
					v_cpuRate.setValue(record.get('cpuRate'));
					v_ramRate.setValue(record.get('ramRate'));					
					v_diskRate.setValue(record.get('diskRate'));		
					v_desc.setValue(record.get('desc'));
					
               		v_button.setText(i18n._('modify'));						
					v_button.setHandler(
					function(){
					var json=getJson(1,record.get('id'));					
					ajax_request(json);				
					});		
					v_window.setTitle(i18n._('modify node'));
					v_window.show();
			  }
   
            },'-',
			{
                itemId: 'removeButton',
                text:i18n._('delete node'),
				//text:i18n._('Delete_ServiceCatalog'),
                icon: '../images/delete.png',
				handler:function(){
				var rows=dataGrid.getSelectionModel().getSelection();
				 var scId;
				 if(rows.length > 0){
				 scId=rows[0].get('id');				 
				 Ext.MessageBox.show({
			                  // title:'提示',
			                 //  msg: '确定删除吗',
							    title: i18n._('notice'),
								msg:i18n._('Are you sure to delete'),
			                   buttons: Ext.MessageBox.YESNO,
			                   icon: Ext.MessageBox.QUESTION,
			                   fn:function(e){
			                	   if(e=="no"){
			                		   return;
			                	   }else if(e=='yes'){
			                	 Ext.Ajax.request({
									url: path+'/sc/node!delete.action',
										method: 'POST',
									params:{
									'node.id':scId
									},
									success: function (response) {
										var result=Ext.JSON.decode(response.responseText);
			           					if(result.success==true){　　　　　　　
									 	showMsg(null,i18n._('delete success'));
										v_store.load();
										//v_newNodeStore.load();
										}else{
											showMsg(null,result.resultMsg);
										}
										}
										});
			                	   }
			                	}
			    		});	 	
					}else{
						/*  Ext.MessageBox.show({
							title: i18n._('notice'),
							msg:i18n._('please choose one column'),
                             icon:Ext.MessageBox.WARNING,
                             buttons: Ext.MessageBox.OK
						}); 
						return; */
						showMsg(null,i18n._('please choose one column')); 
						return;
			    	}		           
			        }
            },
			{xtype:'tbfill'}		
			//{    
			 //   margin:'0 0 0 65',
				//fieldLabel: '套餐名称',
				//fieldLabel:i18n._('ServiceCatalog_name'),
              //  labelWidth: 100,
              //  xtype: 'searchfield',
			//	store: nodeStore
			//}
			
			]}],
    plugins: [
        Ext.create('Ext.grid.plugin.CellEditing', {
            clicksToEdit: 1
        })
    ],
   // height:600,
    width: '100%'
    
})

 Ext.create('Ext.Viewport',{
 layout:'fit',
 items: dataGrid
});


            }
        };
    })();

     MultiLang.init();  
 });

 function getCookie(name){
         var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
         if(arr != null) return unescape(arr[2]);
		 return null;
 }
</script>
 </head>

 <body>
  
 </body>
</html>
